package cn.yotta.user.core.server.service.impl;

import cn.yotta.common.api.model.IdRequest;
import cn.yotta.common.api.model.IdsRequest;
import cn.yotta.common.api.model.Paging;
import cn.yotta.user.api.params.user.UserPageRequest;
import cn.yotta.user.api.params.user.UserStatusChangeRequest;
import cn.yotta.user.api.vo.UserVo;
import cn.yotta.user.core.server.converter.UserConverter;
import cn.yotta.user.core.server.dto.user.UserDto;
import cn.yotta.user.core.server.manager.user.UserManager;
import cn.yotta.user.core.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wyaoyao
 * @date 2023/3/14 8:14
 */
@Component
@Slf4j
public class UserServiceImpl implements UserService {

    private final UserManager userManager;
    private final UserConverter userConverter;

    public UserServiceImpl(UserManager userManager, UserConverter userConverter) {
        this.userManager = userManager;
        this.userConverter = userConverter;
    }

    @Override
    public List<UserVo> findUserByIds(IdsRequest request) {
        List<UserDto> dtos = userManager.findUserByIds(request.getTenantId(), request.getIds());
        if (CollectionUtils.isEmpty(dtos)) {
            return Collections.emptyList();
        }
        return dtos.stream().map(userConverter::dto2Vo).collect(Collectors.toList());
    }

    @Override
    public UserVo findUserById(IdRequest request) {
        UserDto userDto = userManager.findUserById(request.getTenantId(), request.getId());
        return userConverter.dto2Vo(userDto);
    }

    @Override
    public Paging<UserVo> page(UserPageRequest pageRequest) {
        Paging<UserDto> userDtoPaging = userManager.page(pageRequest);
        return userDtoPaging.map(this.userConverter::dto2Vo);
    }

    @Override
    public Boolean freeze(UserStatusChangeRequest request) {
        return userManager.freezeUser(request.getTenantId(),request.getId(),request.getOperatorId());
    }

    @Override
    public Boolean unFreeze(UserStatusChangeRequest request) {
        return userManager.unFreeze(request.getTenantId(),request.getId(),request.getOperatorId());
    }

    @Override
    public Boolean enable(UserStatusChangeRequest request) {
        return userManager.enable(request.getTenantId(),request.getId(),request.getOperatorId());
    }

    @Override
    public Boolean disable(UserStatusChangeRequest request) {
        return userManager.disable(request.getTenantId(),request.getId(),request.getOperatorId());
    }
}
